﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace MemberPoint.Business
{
    public class BaseService<T> where T:class, new()
    {
        protected DbContext db = ContextFactory.GetCurrentContext();

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        #region
        public bool Add(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges() > 0;
        }
        #endregion

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        #region
        public bool Delete(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }
        #endregion

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        #region
        public bool Update(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }
        #endregion

        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="whereLamabda"></param>
        /// <returns></returns>
        #region
        public T Select(Expression<Func<T,bool>> whereLamabda)
        {
            return db.Set<T>().FirstOrDefault(whereLamabda);
        }
        #endregion

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="whereLamabda"></param>
        /// <returns></returns>
        #region
        public IQueryable<T> GetList(Expression<Func<T,bool>> whereLamabda)
        {
            return db.Set<T>().Where(whereLamabda);
        }
        #endregion

        /// <summary>
        /// 查询列表并排序
        /// </summary>
        /// <param name="whereLamabda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        #region
        public IQueryable<T> GetList<Tkey>(Expression<Func<T,bool>> whereLamabda,Expression<Func<T, Tkey>> orderBy,bool isAsc=true)
        {
            if (isAsc)
            {
                //判断是不是升序
                return db.Set<T>().Where(whereLamabda).OrderBy(orderBy);
            }
            else
            {
                //降序排序
                return db.Set<T>().Where(whereLamabda).OrderByDescending(orderBy);
            }
        }
        #endregion

        /// <summary>
        /// 分页查询列表并排序
        /// </summary>
        /// <typeparam name="Tkey"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLamabda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        #region
        public IQueryable<T> GetList<Tkey>(int pageIndex,int pageSize,ref int rowCount,Expression<Func<T,bool>> whereLamabda,
            Expression<Func<T,Tkey>> orderBy, bool isAsc=true)
        {
            //输入页是否小于1，小于1显示第一页，不小于1显示输入的页数
            pageIndex = pageIndex - 1 < 0 ? 1 : pageIndex;
            //页面中显示几条数据，小于10的话显示10条，大于10显示输入条数
            pageSize = pageSize - 1 < 0 ? 10 : pageSize;
            if (isAsc)
            {
                //假如查询11-20条数据，跳过1-10条数据
                return db.Set<T>().Where(whereLamabda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }
            else
            {
                return db.Set<T>().Where(whereLamabda).OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }
        }
        #endregion
    }
}
